<?
    final class Principio_Model_AccountLinkMapper extends Principio_Mapper
    {
        protected static $db_table = null;
        protected static function getDbTableName() { return "AccountLink"; }


        public static function fetch(Principio_Model_Spa $model_spa)
        {
            if ($model_spa->getID() == -1)
            {
                $linked_list = new PrincipioLab_LinkedList();
                for ($i = 0; $i < $model_spa->getModelType()->getExts(); $i++)
                    $linked_list->addItem(new Principio_Model_AccountLink($model_spa, $model_spa->getModelCompany()->createAccount(), $i));

                return $linked_list;
            };

            $values = self::getDBTable()->fetchAll(self::getDBTable()->select()->where("spa_id=?", $model_spa->getID()));

            return Principio_Model_AccountLink::makeCollection([
                'model_spa' => $model_spa,
                'values' => $values->toArray()
            ]);
        }


        public static function delete(Principio_Model_AccountLink $model_account_link)
        {
            self::getDBTable()->delete(['spa_id=?' => $model_account_link->getModelSpa()->getID()]);
            return true;
        }



        /*
         * Set account_id = -1.
         */
        public static function reset(Principio_Model_Account $model_account)
        {
            self::getDBTable()->update(['account_id' => "-1"], ['account_id=?' => $model_account->getID()]);
        }


        public static function insert(Principio_Model_AccountLink $model_account_link)
        {
            $data = [
                'spa_id' => $model_account_link->getModelSpa()->getID(),
                'ext_id' => $model_account_link->getExtNumber(),
                'account_id' => $model_account_link->getModelAccount()->getID()
            ];

            self::getDBTable()->insert($data);

            return true;
        }


        public static function update(Principio_Model_AccountLink $model_account_link)
        {
            self::getDBTable()->update(['account_id' => $model_account_link->getModelAccount()->getID()], ['spa_id=?' => $model_account_link->getModelSpa()->getID(), 'ext_id=?' => $model_account_link->getExtNumber()]);
            return true;
        }
    }
?>